package tw.com.softleader.fire.setting.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import tw.com.softleader.fire.common.dao.GenericDao;
import tw.com.softleader.fire.setting.entity.RoadCodeEntity;
/**
Author:Billy
*/
@Repository
public class RoadCodeDao extends GenericDao<RoadCodeEntity>{
	@Autowired
	private SessionFactory sessionFactory;
	
	@SuppressWarnings("unchecked")
	public List<RoadCodeEntity> findByAddCode(RoadCodeEntity entity){
		
		Session session = sessionFactory.getCurrentSession();
		StringBuffer sql = new StringBuffer();
		
		sql.append("select * from set_road_code where addr_code ilike :addCode");
		Query query = session.createSQLQuery(
				sql.toString()).addEntity(RoadCodeEntity.class);
		
		if( StringUtils.isEmpty(entity.getAddrCode()) ){
			entity.setAddrCode("%");
		} else {
			entity.setAddrCode("%"+entity.getAddrCode()+"%");
		}
		List<RoadCodeEntity> models = 
				query.setString("addCode", entity.getAddrCode()).list();
		return models;
	}
	@SuppressWarnings("unchecked")
	public List<RoadCodeEntity> findByRoadCode(RoadCodeEntity entity){
		
		Session session = sessionFactory.getCurrentSession();
		StringBuffer sql = new StringBuffer();
		
		sql.append("select * from set_road_code where road_code ilike :roadCode");
		Query query = session.createSQLQuery(
				sql.toString()).addEntity(RoadCodeEntity.class);
		
		if( StringUtils.isEmpty(entity.getRoadCode()) ){
			entity.setRoadCode("%");
		} else {
			entity.setRoadCode("%"+entity.getRoadCode()+"%");
		}
		List<RoadCodeEntity> models = 
				query.setString("roadCode", entity.getRoadCode()).list();
		return models;
	}
	@SuppressWarnings("unchecked")
	public List<RoadCodeEntity> findByName(RoadCodeEntity entity){
		
		Session session = sessionFactory.getCurrentSession();
		StringBuffer sql = new StringBuffer();
		
		sql.append("select * from set_road_code where road_name ilike :roadName");
		Query query = session.createSQLQuery(
				sql.toString()).addEntity(RoadCodeEntity.class);
		
		if( StringUtils.isEmpty(entity.getRoadName()) ){
			entity.setRoadName("%");
		} else {
			entity.setRoadName("%"+entity.getRoadName()+"%");
		}
		List<RoadCodeEntity> models = 
				query.setString("roadName", entity.getRoadName()).list();
		return models;
	}
	
}
